#!/usr/local/bin/perl

use strict;

require "dates.pl";

my ($delim,@mon,@sixty,$sec, $min, $hour, $dat, $mn, $year, $dow, $doy, $dlso, $i);
my ($line, @st_date, @st_time,@e_date,@e_time,@proj,@obs,@email,@where,@doy, $DOY);
my ($INFILE, $OUTFILE,$nObs,$date,$time,$st_date,$st_time,$e_date,$e_time,$proj,$obs,$where,$email,$hdrformat,$dataformat);
$delim = "|";

@mon = qw(01 02 03 04 05 06 07 08 09 10 11 12);
@sixty = qw(00 01 02 03 04 05 06 07 08 09
            10 11 12 13 14 15 16 17 18 19
            20 21 22 23 24 25 26 27 28 29 
            30 31 32 33 34 35 36 37 38 39 
            40 41 42 43 44 45 46 47 48 49
            50 51 52 53 54 55 56 57 58 59);
($sec, $min, $hour, $dat, $mn, $year, $dow, $doy, $dlso) = localtime();
$year += 1900;
$hour -= $dlso;
$date = $year.'-'.$mon[$mn].'-'.$dat;
$time = $hour.':'.$sixty[$min];
$DOY = getDOY($date, $time);
$i=0;
open (INFILE, 'rem_obs.txt') or die "Can't open file: $!\n";

while (<INFILE>) 
{
    $line = $_ ;
    if (substr($line,0,1) ne '#')
    {
        $st_date[$i] = substr($line,2,10);
        $st_time[$i] = substr($line,13,5);
        $e_date[$i] = substr($line,19,10);
        $e_time[$i] = substr($line,30,5);
        $proj[$i] = substr($line,36,5);
        $obs[$i] = substr($line,42,25);
        $obs[$i] =~ s/\s+$//;
        $email[$i] = substr($line,67,27);
        $email[$i] =~ s/\s+$//;
        $where[$i] = substr($line,95,18);
        $where[$i] =~ s/\s+$//;
	 # this could be done with std perl lib stuff -
	 # use Time::Local;
	 # $t = timelocal( @dateelements );
	 # @b = (localtime($t))[1,2,7]; 
	 # $doy[$i] = $b[0]/3600 + $b[1]/60 + $b[4];
        $doy[$i] = getDOY(@st_date[$i], @st_time[$i]);
        if ($doy[$i] > $DOY)
        {
            $i++;
        }
    }
}
close INFILE;
$nObs = $i--;

$st_date = "2006-03-08";
$st_time = "14:00";
$doy = getDOY($st_date, $st_time);
$e_date = "2006-03-09";
$e_time = "02:00";
$proj = "C1341";
$obs = "Baerbel Koribalski";
$email = "Baerbel.Koribalski\@csiro.au";
$where = "";

$i = $nObs;
while (($doy < $doy[$i-1]) && ($i > 0)) 
{
    $st_date[$i] = $st_date[$i-1];
    $st_time[$i] = $st_time[$i-1];
    $e_date[$i] = $e_date[$i-1];
    $e_time[$i] = $e_time[$i-1];
    $proj[$i] = $proj[$i-1];
    $obs[$i] = $obs[$i-1];
    $email[$i] = $email[$i-1];
    $where[$i] = $where[$i-1];
    $doy[$i] = $doy[$i-1];
    $i--;
}

$st_date[$i] = $st_date;
$st_time[$i] = $st_time;
$e_date[$i] = $e_date;
$e_time[$i] = $e_time;
$proj[$i] = $proj;
$obs[$i] = $obs;
$email[$i] = $email;
$where[$i] = $where;
$doy[$i] = $doy;

$nObs++;

open (OUTFILE, ">rem_obs.out") or die "Can't open file: $!\n";
print OUTFILE "# Remote Observing text file\n";

$hdrformat=sprintf("#%s%%-16s%s%%-16s%s%%-6s%s%%-30s%s%%-30s%s%%-30s\n",  " ",$delim,$delim,$delim,$delim,$delim,$delim);
$dataformat=sprintf(" %s%%-16s%s%%-16s%s%%-6s%s%%-30s%s%%-30s%s%%-30s\n",  " ",$delim,$delim,$delim,$delim,$delim,$delim);
printf(OUTFILE $hdrformat, "Start", "End", "Proj", "Observer", "Email Address", "Observing Location (if not ATNF)");

$i = 0;
while ($i < $nObs)
{
    printf(OUTFILE $dataformat, $st_date[$i]."_".$st_time[$i],
			        $e_date[$i]."_".$e_time[$i],
			        $proj[$i],
			        $obs[$i],
			        $email[$i],
			        $where[$i]);
   $i++;
}
print OUTFILE "# All times AEST\n";
print OUTFILE "# Last updated $year\-$mon[$mn]\-$dat $hour:$sixty[$min]:$sixty[$sec]\n"; 
close(OUTFILE);

